package com.zzx.graduation.controller;


import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.zzx.graduation.entity.News;
import com.zzx.graduation.response.Constants;
import com.zzx.graduation.response.ResponseMessage;
import com.zzx.graduation.response.StateCode;
import com.zzx.graduation.service.FileService;
import com.zzx.graduation.service.GroupChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.List;


//@Transactional
@RestController
@CrossOrigin
@Transactional
@RequestMapping("/api/file")
public class FileController {

    //slf4j.Logger
    private  static final Logger log = LoggerFactory.getLogger(FileController.class);

    @Autowired
   private FileService fileService;

    /**
     * IMAGE = "image";
     * VIDEO = "video";
     * TEXT = "text";
     * OTHER_FILE = "other_file";
     * @param name 文件名
     * @param response 响应体
     * @return
     */
    @GetMapping(value = "/download/{name}")
    public void fileDownload(@PathVariable("name") String name,
                             HttpServletResponse response) {

        try {
            fileService.fileDownload( name, response);
        } catch (Exception e) {
            e.printStackTrace();
            //return   new ResponseMessage<>(StateCode.ERROR,"下载失败",null);
        }
      //  return   new ResponseMessage<>(StateCode.SUCCESS,"下载成功",null);
    }

    @PostMapping(value = "/upload")
    public ResponseMessage upload(@RequestParam("file") MultipartFile file) {
        String path = null;
        try {
            path = fileService.upload(file);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseMessage<String>(StateCode.ERROR,"upload fail，可能是文件已经存在",null);
        }
        System.out.println( "上传路径：" + path);
        return new ResponseMessage<String>(StateCode.SUCCESS,"upload成功",path);
    }

}
